import axios from 'axios';
import api from '../request/api';
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate';
Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    token: process.env.NODE_ENV == 'development' ? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuaWQiOiJvN3RrMjYyM194eE4xdllsa0Rick1JQlNzeU04IiwidGltZSI6MTY4NjkwNDU4MywidXNlcl9pZCI6NzkwLCJuaWNrX25hbWUiOiJcdTU0MzRcdTZkYzdcdTk1MGIiLCJ0eXBlIjoib25saW5lIiwiZXhwIjoxNjk1NTQ0NTgzfQ.iZCrS6yiuvfAt1v23uiRbHqzLld1G_-viN3fZPqKqxw' : null,
    // token: process.env.NODE_ENV == 'development' ? 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJvcGVuaWQiOiJvN0E5RXVDQWd4UmdVOVVvTTI4SlhHWmdiRFk4IiwidGltZSI6MTY4MTExMDgyNSwidXNlcl9pZCI6MzMzLCJuaWNrX25hbWUiOiJcdTE0ZmUiLCJ0eXBlIjoib25saW5lIiwiZXhwIjoxNjgxMTk3MjI1fQ.3IwXZLA-LJbXpDGp9W4VNx87bSoSxUXWz0_ziueyWMI' : null,

    // token: null,
    userInfo: {},
    orderTab: null,
    product_id: '',
    integral: '',
    stock_id: "",
    stepper: "",
    cart_id: "",
    image: [],
    content: ''
  },
  mutations: {
    setImage (state, value) {
      state.image = value
    },
    setContent (state, value) {
      state.content = value
    },
    setUserInfo (state, value) {
      state.userInfo = value
    },
    settoken (state, value) {
      state.token = value
    },
    setOrderTab (state, value) {
      state.orderTab = value
    },
    setProductid (state, value) {
      state.product_id = value
    },
    setintegral (state, value) {
      state.integral = value
    },
    setstock_id (state, value) {
      state.stock_id = value
    },
    setstepper (state, value) {
      state.stepper = value
    },
    setcart_id (state, value) {
      state.cart_id = value
    },
  },
  actions: {
    getUserInfo ({ state, commit }) {
      if (state.token) {
        return axios.get(api.GetUserAllInfo).then((res) => {
          const userInfo = res.data.data.data
          // if (userInfo.avatar) {
          //   userInfo.avatar = `http://bfyc.jiazhengdashi.com/${userInfo.avatar}`
          // }
          commit('setUserInfo', userInfo)
        })
      } else {
        // 没登录就去掉之前的localStorage
        localStorage.clear()
      }
    },
  },
  modules: {
  }
})
